package ru.store.paysystem;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Created with IntelliJ IDEA.
 * User: andrebrov
 * Date: 12.02.13
 * Time: 20:32
 */
public class PaySystem implements IPaySystem {
	private static int year = 2012;
	private static String vesrion_num = "325435";
	private static String operatins = "cash";
	private static float rate = 31f;
	
	private static Logger log = Logger.getLogger(PaySystem.class.getName());
	
	Connection connection = null;
	
    public PaySystem(Connection connection)
	{
		this.connection = connection;
	}

    public Connection getConnection() {
    	return connection;
    }
    
	@Override
    public void increaseProfit(float amount) throws ExceptionInvalidAmount {
        if (amount > 0) {
            double amountUSD = amount / rate;
            log.log(Level.INFO, "Increasing profit for $%f", amount);
            try {
                connection.setAutoCommit(false);
                PreparedStatement ps = connection.prepareStatement(
                		"insert into cash (year,version_num,operatins,summ) "+
                		"values (?,?,?,?)"
                		);
                ps.setString(1, String.valueOf(year));
                ps.setString(2, vesrion_num);
                ps.setString(3, operatins);
                ps.setString(4, String.valueOf(amountUSD));
                ps.execute();
                connection.commit();
            } catch (Exception e) {
                try {
					connection.rollback();
				} catch (SQLException e1) {
	                e.printStackTrace();
				}
                e.printStackTrace();
            }
            log.log(Level.INFO, "Profit for $%f increased", amount);
        }
    }

	@Override
	public void setAmount(float amount) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public float getAmount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean getIsCalled(boolean clearFlag) {
		// TODO Auto-generated method stub
		return false;
	}

}
